<?php
  include('header.php');
  include('functions.php');
  $pageTitle = $l_forumname;
  $result = $db->query("
SELECT 
  cat_id,
  cat_name,
  cat_description 
FROM ".$table_prefix."categories WHERE cat_parent = 0
ORDER BY cat_sort,cat_id ASC");

  if (!$result) {
      echo "An error occured! Cant connect to database!";
      die;
  }
  $qqq += 1;
  //echo "<table bgcolor='black' style='border:3px solid #000;'><tr><td>";
  
  while ($row = $db->fetch_array($result,'assoc')) {
      $div = "div" . $row['cat_id'];
      $img = "image" . $row['cat_id'];
      $tags = array('{CATEGORY}', '{F_ID}', '{I_ID}');
      $data = array($row['cat_name'], $div, $img);
      echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/categories.html"));
      $result2 = $db->query("
SELECT 
	  cat_id,
	  cat_name,
	  cat_description,
	  cat_permission,
	  cat_posts,
	  cat_topics,
	  cat_last_poster_id,
	  cat_last_post_id,
	  cat_last_poster_name,
	  cat_last_post_subject,
	  cat_last_post_time,
	  cat_icon
FROM ".$table_prefix."categories 
WHERE 
cat_parent = $row[cat_id]
ORDER BY cat_sort,cat_id ASC");
      $qqq += 1;
      while ($row2 = $db->fetch_array($result2,'assoc')) {
          //FORUM PERMISSION
          if ($row2['cat_permission'] == 3 and isset($_SESSION['signed_in']) and $_SESSION['user_level'] >= 1) {
              $hide_forum = 0;
          }
          if ($row2['cat_permission'] == 3 and isset($_SESSION['signed_in']) and $_SESSION['user_level'] < 1) {
              $hide_forum = 1;
          }
          if ($row2['cat_permission'] == 3 and !isset($_SESSION['signed_in'])) {
              $hide_forum = 1;
          }
          if ($row2['cat_permission'] == 2 and !isset($_SESSION['signed_in'])) {
              $hide_forum = 1;
          }
          if ($row2['cat_permission'] == 2 and isset($_SESSION['signed_in'])) {
              $hide_forum = 0;
          }
          
          if ($row2['cat_permission'] <= 1) {
              $hide_forum = 0;
          }
          
          
          if ($hide_forum != 1) {
              $tags = array('{FORUM_IMG}', '{FORUM_ID}', '{FORUM_NAME}', '{FORUM_DESC}', '{I_ID}', '{F_ID}');
              $data = array('<img src="./style/' . $default_style . '/img/' . $row2['cat_icon'] . '" alt="" align="middle">', $row2['cat_id'], $row2['cat_name'], $row2['cat_description'], $img, $div);
              echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/forums.html"));
              
              
              //HERE WILL GOES CODE FOR CACHING LAST TOPIC - this is just test for caching
              $num_replies = $row2['cat_posts'];
              $topics = $row2['cat_topics'];
              if (file_exists("./cache/forum" . $row2['cat_id'] . ".php") and $forum_cache == 1) {
                  $topicid = get_value_of('TopicID', './cache/forum' . $row2['cat_id'] . '.php');
                  $topicid = trim($topicid);
                  $topicsubject = '' . get_value_of('Last Topic', './cache/forum' . $row2['cat_id'] . '.php');
                  $topicsubject = trim($topicsubject);
                  $lastpost_date = get_value_of('Topic date', './cache/forum' . $row2['cat_id'] . '.php');
                  $topics = get_value_of('Topics', './cache/forum' . $row2['cat_id'] . '.php');
                  $num_replies = get_value_of('Replies', './cache/forum' . $row2['cat_id'] . '.php');
                  $last_user = get_value_of('Topic by', './cache/forum' . $row2['cat_id'] . '.php');
                  $last_userid = get_value_of('UserID', './cache/forum' . $row2['cat_id'] . '.php');
                  
                  $last_poster = "<a class='last_poster' href = 'user.php?u=" . $last_userid . "'>" . $last_user . "</a>";
                  if ($last_userid <= 1) {
                      $last_poster = "<i>" . $last_user . "</i>";
                  }
                  
                  $tags = array('{$T_PATH}', '{TOPIC_ID}', '{TOPIC_SUBJECT}', '{TOPIC_LAST_POST}', '{TOPIC_DATE}', '{NUM_TOPICS}', '{NUM_REPLIES}', '{AUTHOR}', '{ON}');
                  
                  $data = array('<img title="View the latest post" border=0 src="./style/' . $default_style . '/img/icon_topic_latest.gif">', $topicid, $topicsubject, $last_poster, $lastpost_date, $topics, $num_replies, $l_sauthor, $l_on);
                  
                  echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/forum_topics.html"));
                  echo "</table>";
              } else {                 
                  $last_poster = "<a class='last_poster' href = 'user.php?u=" . $row2['cat_last_poster_id'] . "'>" . $row2['cat_last_poster_name'] . "</a>";
                  if ($row2['cat_last_poster_id'] <= 1) {
                      $last_poster = "<i>" . $row2['cat_last_poster_name'] . "</i>";
                  }
                  
                  $topicid = $row2['cat_last_post_id'];
                  $topicsubject = trim($row2['cat_last_post_subject']);
                  $lastpost_date = date($date_format, strtotime($row2['cat_last_post_time']));
				  
				  if (date("d.m.Y", strtotime($row2['cat_last_post_time'])) == $date->format("d.m.Y")) {
				  $lastpost_date = "<b>$l_today</b>, " . date("H:i:s", strtotime($row2['cat_last_post_time']));
							  }
					if (date("d.m.Y", strtotime($row2['cat_last_post_time'])) == date("d.m.Y",$yesterday)) {
				  $lastpost_date = "<b>$l_yesterday</b>, " . date("H:i:s", strtotime($row2['cat_last_post_time']));
							  }		  
                  
                  $tags = array('{$T_PATH}', '{TOPIC_ID}', '{TOPIC_SUBJECT}', '{TOPIC_LAST_POST}', '{TOPIC_DATE}', '{NUM_TOPICS}', '{NUM_REPLIES}', '{AUTHOR}', '{ON}');
                  
                  $data = array('<img title="'.$l_view_latest .'" border=0 src="./style/' . $default_style . '/img/icon_topic_latest.gif">', $topicid, $topicsubject, $last_poster, $lastpost_date, $topics, $num_replies, $l_sauthor, $l_on);
                  
                  if ($row2['cat_last_post_subject'] == "") {
                      echo '';
                      $last_poster = "";
                      $topicid = "";
                      $lastpost_date = "";
                      $topicsubject = "";
                      
                      $data = array('', '', '', '', '', '-', '-', '</b>' . $l_no_topics_index . '', '');
                  }
                  
                  echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/forum_topics.html"));
                  echo "</table>";
              }
          }
          //END - HERE WILL GOES CODE FOR CACHING LAST TOPIC
		  
          //END PERMISSION
          echo "</tr></table><!--end table-->";
      }
  }
						  if ($recent_posts_on_index>=1)
						  {  
						  if ($recent_posts_on_index>=30) {$recent_posts_on_index=30;}
						  //Prepare query for mods and admins (show unnapproved posts) or NOT for other users
						  $check_approved = "AND post_approve = 0";
						  if ($_SESSION['user_level']>=1)
						  {$check_approved = "";}
$recent_topics = $db->query("
						  SELECT 
						  " . $table_prefix . "posts.post_topic,
						  " . $table_prefix . "posts.post_id, 
						  " . $table_prefix . "posts.post_subject, 
						  " . $table_prefix . "posts.post_date, 
						  " . $table_prefix . "posts.post_by,
						  " . $table_prefix . "posts.post_page,
						  " . $table_prefix . "posts.post_approve,
						  " . $table_prefix . "users.user_id, 
						  " . $table_prefix . "users.user_name, 
						  " . $table_prefix . "users.user_level
						  FROM 
						  " . $table_prefix . "posts 
						  LEFT JOIN 
						  " . $table_prefix . "users 
						  ON 
						  " . $table_prefix . "posts.post_by = " . $table_prefix . "users.user_id 
						  WHERE " . $table_prefix . "posts.post_by !=1 $check_approved ORDER BY 
						  post_date DESC LIMIT 0,$recent_posts_on_index") or trigger_error("SQL", E_USER_ERROR);
						  
                          echo "<br><table style='width:100%;margin-left:1px'>
						  <tr><th><b>&nbsp;$l_recent_posts</b></th></tr>";
                          while ($row = $db->fetch_array($recent_topics,'assoc')) {
                          $page = $row['post_page'];
                          if($page <= 0) {$page=1;}
						  $post_subject = trim($row['post_subject']);
						  if ($row['post_approve']==1)
						  {$post_subject = "<span style=''>$post_subject [$l_unnaproved_posts]</span>";}
						  
						  $post_date = date($date_format,strtotime($row['post_date']));
						  
						  if (date("d.m.Y", strtotime($row['post_date'])) == $date->format("d.m.Y")) {
				  $post_date = "$l_today, " . date("H:i:s", strtotime($row['post_date']));
							  }
						  if (date("d.m.Y", strtotime($row['post_date'])) == date("d.m.Y",$yesterday)) {
                  $post_date = "$l_yesterday, " . date("H:i", strtotime($row['post_date'])) . "";
                   }  
						  
                          echo "
                          <tr><td height='23px'>&nbsp;
						  <a href='topic.php?t=$row[post_topic]&page=$page#$row[post_id]'><b>$post_subject</b></a>
                           $l_by <a href='user.php?u=$row[user_id]'><b>$row[user_name]</b></a> 
                          $l_at $post_date<br>
                          </td></tr>";
                          }
                          echo "</table>";
                          }
  include 'footer.php';
  // Get all the page's HTML into a string
  $pageContents = ob_get_contents();
  // Wipe the buffer 
  ob_end_clean();
  echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
?>